//
// Created by weslywang on 2020/4/30.
//
#include "../common/common.h"

Node *connect(Node *root) {
    if (!root) {
        return NULL;
    }
    queue<Node *> list;
    Node *front, *pos = root;
    list.push(root);
    while (!list.empty()) {
        front = list.front();
        list.pop();
        if (front->left && front->right) {
            list.push(front->left);
            list.push(front->right);
        }
        if (front == pos) {
            pos = front->right;
            front->next = NULL;
        } else {
            front->next = list.front();
        }
    }
    return root;
}

int main() {
    Common common;
    int a[] = {1, 2, 3, 4, 5};
    vector<vector<int>> numList = common.loadData();
    for (int i = 0; i < numList.size(); i++) {
        TreeNode *root = common.makeTree(numList[i]);
        Node *node = common.treeNodeToNode(root);
        connect(node);
        cout << "**************" << endl;
    }
    return 0;
}